//
//  PTAnimationIndicator.m
//  PlanTech
//
//  Created by ruifeng song on 2018/6/19.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "PTAnimationIndicator.h"
@implementation PTAnimationIndicator

-(id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _isAnimating = NO;
        
//        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        self.layer.cornerRadius = 14;
        self.layer.masksToBounds = YES;
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
        [self addSubview:imageView];
//        [view addSubview:imageView];
        
        _imageView = imageView;
        _imageView.animationImages = [self getImageArray];
        
//        _backView = view;
        
//        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_imageView.frame)+ 10, ScreenSize.width, 20)];
        
//        _label.textAlignment = NSTextAlignmentCenter;
//        _label.textColor = [UIColor lightGrayColor];
//        _label.font = [UIFont systemFontOfSize:13];
//        [self addSubview:_label];
        self.layer.hidden = YES;
    }
    return self;
}

-(NSArray *)getImageArray
{
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i<= 8; i++) {
        NSString *imageName = [NSString stringWithFormat:@"loading_img%d.png",i];
        
        //性能优化，直接以文件路径的形式创建image,没有缓存，自动释放，imageNamed:有缓存 - 退出程序释放.
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"loadingimages" ofType:@"bundle"];
        NSString * imagePath =[NSString stringWithFormat:@"%@/%@",path,imageName];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        [imageArray addObject:image];

    }
    
    return imageArray;
}

-(void)startAnimation
{
    _isAnimating = YES;
    self.layer.hidden = NO;
    [self doAnimation];
}

-(void)doAnimation
{
    //设置动画总时间
    self.imageView.animationDuration = 1;
    //设置重复次数,0表示无限
    self.imageView.animationRepeatCount = 0;
    
    //开始动画
    [self.imageView startAnimating];
}

-(void)stopAnimation
{
    _isAnimating = NO;
    [self.imageView stopAnimating];
    self.imageView.animationImages = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
